<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mysql
 *
 * @author Implement <hdjahs@qq.com>
 */
class mysql {

    private static $ins = null;
    private $mysqliObj;

    final private function __construct() {
        $config = new Config();
        $this->mysqliObj = new mysqli($config['db']['host'], $config['db']['username'], $config['db']['password'], $config['db']['database']);
        if (mysqli_connect_errno()) {
            echo "连接失败" . mysqli_connect_error();
            exit();
        }
    }

    public static function getIns() {
        if (self::$ins === null) {
            self::$ins = new self();
        }
        return self::$ins;
    }

    public function select($sql) {
        $result = $this->mysqliObj->query($sql);
        $array = array();
        if ($result) {
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                    $array[] = $row;
                }
            }
        }
        return $array;
    }

    public function update($sql) {
        $result = $this->mysqliObj->query($sql);
        return $result;
    }
    
    public function insert($sql) {
        $this->mysqliObj->query($sql);
        $id = $this->mysqliObj->insert_id;
        return $id;
    }

    public function delete($sql) {
        $result = $this->mysqliObj->query($sql);
        return $result;
    }

    final protected function __clone() {
        
    }

}
